package com.food.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.food.dao.service.FoodsaveService;
import com.food.dao.service.RecipeService;
import com.food.model.Foodsave;
import com.food.model.Recipe;

/**
 * Servlet implementation class SearchRecipe
 */
@WebServlet("/recipe/SearchRecipe")
public class SearchRecipe extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");
		
		String pageNumberStr = "0";
		if (request.getParameter("pageNumber")!=null) {
			pageNumberStr = request.getParameter("pageNumber");
		}

		int pageNumber = 0;
		
		String search = "";
		
		
		if (request.getParameter("search")!=null && request.getParameter("search").trim().length()!=0 && request.getParameter("searchbtn").equals("查詢食譜")) {
			
			
			
			search = request.getParameter("search");
			
			System.out.println(search);
			System.out.println(request.getParameter("searchbtn"));
			System.out.println("in");
			
		request.removeAttribute("now");
		request.removeAttribute("pre");
		request.removeAttribute("next");
		request.removeAttribute("last");
		
		
		RecipeService pageService = new RecipeService();
		
		/* 建立 List<Product> */
		List<Recipe> Beanlist = new ArrayList<Recipe>();

		/* 從服務取得 物件集合 */
		// Beanlist = prodsv.findAllProduct();

		// 分頁6筆

		pageNumber = Integer.parseInt(pageNumberStr);
		request.setAttribute("now", pageNumber);
		request.setAttribute("first", "/ViewRecipeServlet?pageNumber=0");
		request.setAttribute("pre", "/ViewRecipeServlet?pageNumber=" + (pageNumber - 1));
		request.setAttribute("next","/ViewRecipeServlet?pageNumber="+( pageNumber + 1));
		
		
		Recipe recipe = new Recipe();
		recipe.setRname(search);
		
		//每頁6筆，頁數
		Beanlist = pageService.findRecipeByName(recipe);
		
		

		
		List<Recipe> recoll = pageService.findAllRecipe();	
		
		int last = recoll.size()/9;
		request.setAttribute("toEnd", "/ViewRecipeServlet?pageNumber="+last);
		request.setAttribute("total", last);
		
		if(last==pageNumber){
		request.setAttribute("last", last);
		}

		request.setAttribute("recipe_show", Beanlist);
		RequestDispatcher rd = request
				.getRequestDispatcher("/recipe/searchRecipe.jsp");
		rd.forward(request, response);
		return;
		}
		else if(request.getParameterValues("category")!=null && request.getParameter("searchbtn").equals("送出查詢")){
			
			
			
			String[] category = request.getParameterValues("category");
			System.out.println(category.toString());
			
		request.removeAttribute("now");
		request.removeAttribute("pre");
		request.removeAttribute("next");
		request.removeAttribute("last");
		
		Set<Integer> categorySet = new HashSet<Integer>();
		FoodsaveService foodsaveService = new FoodsaveService();
		List<Foodsave> foodsaveList = new ArrayList<Foodsave>();
		for(String c:category){
			Foodsave foodsave = new Foodsave();
			foodsave.setFsid(Integer.parseInt(c));
			foodsaveList = foodsaveService.findFoodsaveByFoodsearch(foodsave);
			for(Foodsave f:foodsaveList){
				categorySet.add(f.getRid());
				
			}
		}
		
		
		
		
		RecipeService pageService = new RecipeService();
		
		/* 建立 List<Product> */
		List<Recipe> Beanlist = new ArrayList<Recipe>();

		/* 從服務取得 物件集合 */
		// Beanlist = prodsv.findAllProduct();

		// 分頁6筆

		pageNumber = Integer.parseInt(pageNumberStr);
		request.setAttribute("now", pageNumber);
		request.setAttribute("first", "/ViewRecipeServlet?pageNumber=0");
		request.setAttribute("pre", "/ViewRecipeServlet?pageNumber=" + (pageNumber - 1));
		request.setAttribute("next","/ViewRecipeServlet?pageNumber="+( pageNumber + 1));
		
		
		for(Integer r:categorySet){
			Recipe recipe = new Recipe();
			recipe.setRid(r);
			recipe = pageService.findRecipeById(recipe);
			Beanlist.add(recipe);
		}

		
		List<Recipe> recoll = pageService.findAllRecipe();	
		
		int last = recoll.size()/9;
		request.setAttribute("toEnd", "/ViewRecipeServlet?pageNumber="+last);
		request.setAttribute("total", last);
		
		if(last==pageNumber){
		request.setAttribute("last", last);
		}

		request.setAttribute("recipe_show", Beanlist);
		RequestDispatcher rd = request
				.getRequestDispatcher("/recipe/searchRecipe.jsp");
		rd.forward(request, response);
		return;
			
		}
		
		
		else{
			System.out.println(request.getParameter("searchbtn"));
			System.out.println(request.getParameterValues("category"));
			System.out.println("123");
			//request.setAttribute("recipe_show", Beanlist);
			RequestDispatcher rd = request
					.getRequestDispatcher("/ViewRecipeServlet?pageNumber=0");
			rd.forward(request, response);
			return;
		}
		
		
		
	}

}
